package com.manage.modules.controller;

import cn.hutool.core.util.StrUtil;
import com.manage.common.annotation.Log;
import com.manage.common.constant.UserConstants;
import com.manage.common.core.controller.BaseController;
import com.manage.common.core.domain.AjaxResult;
import com.manage.common.enums.BusinessType;
import com.manage.modules.domain.TBaseArea;
import com.manage.modules.domain.TBaseStationType;
import com.manage.modules.service.ITBaseStationTypeService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;

/**
 * 站点分类Controller
 *
 * @author xu
 * 2021/8/11 17:37
 */
@Api(value = "站点分类控制器", tags = {"站点分类管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/modules/stationType")
public class TBaseStationTypeController extends BaseController {
    @Resource
    private ITBaseStationTypeService typeService;

    /**
     * 获取站点分类下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(TBaseStationType baseStationType) {
        List<TBaseStationType> baseStationTypes = typeService.selectStationTypeList(baseStationType);
        return AjaxResult.success(typeService.buildStationTypeTreeSelect(baseStationTypes));
    }

    /**
     * 查询站点分类列表（排除节点）
     */
    @PreAuthorize("@ss.hasPermi('modules:stationType:list')")
    @GetMapping("/list/exclude/{stationTypeId}")
    public AjaxResult excludeChild(@PathVariable(value = "stationTypeId", required = false) Long stationTypeId) {
        List<TBaseStationType> stationTypes = typeService.selectStationTypeList(new TBaseStationType());
        Iterator<TBaseStationType> it = stationTypes.iterator();
        while (it.hasNext()) {
            TBaseStationType d = (TBaseStationType) it.next();
            if (d.getStationTypeId().intValue() == stationTypeId
                    || ArrayUtils.contains(StrUtil.split(d.getAncestors(), ","), stationTypeId + "")) {
                it.remove();
            }
        }
        return AjaxResult.success(stationTypes);
    }

    /**
     * 根据站点分类编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('modules:stationType:query')")
    @GetMapping(value = "/{stationTypeId}")
    public AjaxResult getInfo(@PathVariable Long stationTypeId) {
        return AjaxResult.success(typeService.selectStationTypeById(stationTypeId));
    }

    /**
     * 删除站点分类
     */
    @PreAuthorize("@ss.hasPermi('modules:stationType:remove')")
    @Log(title = "站点分类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{stationTypeId}")
    public AjaxResult remove(@PathVariable Long stationTypeId) {
        if (typeService.hasChildByTypeId(stationTypeId)) {
            return AjaxResult.error("存在下级站点分类,不允许删除");
        }
        return toAjax(typeService.deleteStationTypeById(stationTypeId));
    }

    /**
     * 获取站点分类列表
     */
    @PreAuthorize("@ss.hasPermi('modules:stationType:list')")
    @GetMapping("/list")
    public AjaxResult list(TBaseStationType baseStationType) {
        List<TBaseStationType> baseStationTypeList = typeService.selectStationTypeList(baseStationType);
        return AjaxResult.success(baseStationTypeList);
    }

    /**
     * 新增站点分类
     */
    @PreAuthorize("@ss.hasPermi('modules:stationType:add')")
    @Log(title = "站点分类管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TBaseStationType baseStationType) {
        if (UserConstants.NOT_UNIQUE.equals(typeService.checkStationTypeNameUnique(baseStationType))) {
            return AjaxResult.error("新增站点分类'" + baseStationType.getStationTypeName() + "'失败，站点分类名称已存在");
        }
        return toAjax(typeService.insertStationType(baseStationType));
    }

    /**
     * 修改站点分类
     */
    @PreAuthorize("@ss.hasPermi('modules:stationType:edit')")
    @Log(title = "站点分类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody TBaseStationType baseStationType) {
        if (UserConstants.NOT_UNIQUE.equals(typeService.checkStationTypeNameUnique(baseStationType))) {
            return AjaxResult.error("修改站点分类'" + baseStationType.getStationTypeName() + "'失败，站点分类名称已存在");
        } else if (baseStationType.getParentId().equals(baseStationType.getStationTypeId())) {
            return AjaxResult.error("修改站点分类'" + baseStationType.getStationTypeName() + "'失败，上级站点分类不能是自己");
        } else if (StrUtil.equals(UserConstants.DEPT_DISABLE, baseStationType.getStationTypeState() + "")
                && typeService.selectNormalChildrenStationTypeById(baseStationType.getStationTypeId()) > 0) {
            return AjaxResult.error("该站点分类包含未停用的子站点分类！");
        }
        return toAjax(typeService.updateStationType(baseStationType));
    }
}
